package edu.java.lectures.lec12.threads.synchronization.dynamic.msgbox;

public class MsgReader extends Thread {

    private MsgBox msgBox;

    public MsgReader(String name, MsgBox msgBox) {
	super(name);
	this.msgBox = msgBox;
    }

    public MsgReader(String name, Runnable runnableObj, MsgBox msgBox) {
	super(runnableObj, name);
	this.msgBox = msgBox;
    }

    @Override
    public void run() {
	while (true) {
	    String msg = null;
	    try {
		msg = msgBox.getMessage();
	    } catch (InterruptedException e) {
		e.printStackTrace();
		return;
	    }
	    System.out.println("Reader " + getName()
		    + " has just read the message: [" + msg + "]");
	}
    }
}
